package com.ns.school.service.edu.impl;

import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.edu.LendBookDB;
import com.ns.school.mapper.edu.LendBookMapper;
import com.ns.school.service.edu.LendBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service

public class LendBookServiceImpl implements LendBookService {
    @Autowired
    private LendBookMapper lendBookMapper;
//    @Override
//    public Integer lendBookAc(LendBookDB lendBookDB) {
//        return lendBookMapper.lendBook(lendBookDB);
//    }

    @Override
    public Integer lendBookAc(LendBookDB lendBookDB) {
        return null;
    }

    @Override
    public BookDB findBookQty(BookDB bookDB) {
        return lendBookMapper.findBookQty(bookDB);
    }

    @Override
    public Integer auditTrue(BookDB bookDB) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void lendBook(LayuiResult<LendBookDB> result, BookDB bookDB, LendBookDB lendBookDB) {
        Integer lendbook = lendBookMapper.lendBook(lendBookDB);
        if (lendbook > 0) {
            result.setCode(0);
            result.setMsg("借阅成功");
            Integer booklendac=lendBookMapper.auditTrue(bookDB);
            if(bookDB.getBookQty()<0){
                result.setCode(400);
                result.setMsg("借阅失败,库存不足");
                //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new RuntimeException("库存不足");
            }

        } else {
            result.setCode(500);
            result.setMsg("借阅失败");

        }
    }
    }

//    @Override
//    public Integer auditTrue(BookDB bookDB) {
//        return lendBookMapper.auditTrue(bookDB);
//    }




